// pages/act/home/home.js
let {
  api_uc_GetInfoByToken,
  api_GetAllBanners,
  api_GetAllAuthorities,
  api_GetActList,
  api_LikeAct
} = require("../../api/getData.js")
const app = getApp()

Page({
  data: {
    swiperList: [],
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    previousMargin: 0,
    nextMargin: 0,
    dataSource: [],
    isloading: false,
    // 上拉加载
    curPage: 0,
    maxSize: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("home页面被加载了")
    this.setData({
      swiperList: [{
        url: '',
        img: '/images/swipper/1.jpg',
        title: '学院路校区'
      }, {
        url: '',
        img: '/images/swipper/2.jpg',
        title: '图书馆'
      }, {
        url: '',
        img: '/images/swipper/3.jpg',
        title: '沙河校区'
      }]
    })
   
    /*获取主页活动列表*/
    this.getHomeActList();
  },

  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作
    this.refresh();
  },
  onReachBottom: function() {
    // 页面相关事件处理函数--监听下拉触底
    this.getHomeActList();
  },
  /**************以上是生命周期********************以下是自定义函数**************************/

  /*获取主页活动列表*/
  getHomeActList: function() {
    console.log("==> 加载函数执行")
    console.log("bottom: ", this.data.bottom)
    if (this.data.bottom >= this.data.maxSize) {
      console.log("==> 到达底部");
      wx.showToast({
        title: '别再拉了，我是有底线的！',
        icon: 'none',
      })
      return
    } else {
      let params = {
        page: this.data.curPage + 1,
        size: 5
      };
      let weishalong = this.data.dataSource
      api_GetActList(params, (res) => {
        console.log("#########--活动列表--##########", res.data)
        //下拉刷新停止
        wx.stopPullDownRefresh()
        res.data.forEach(i => {
          var tmp = i;
          tmp.imageUrl = app.globalData.static_url+"topic/"+tmp.id+".png";
          weishalong.push(i)
        })
        this.setData({
          bottom: weishalong.length,
          dataSource: weishalong,
          curPage: 1,
          maxSize: 191,
        })
      })
    }
  },

  bindMakeComment: function (e) {
    var topic = e.currentTarget.dataset.index;
    console.log('当前点击的活动ID==>', topic.id);
    var dataTmp = JSON.stringify(topic);
    wx.navigateTo({
      url: '../../mine/question/question?topic=' + dataTmp,
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
    })
  },

})